package com.aptemo.webetek.server.data;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.aptemo.webetek.shared.LabelBean;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class BetLabel
{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key		key;

	private String	sid;

	// unowned N : 1 relationship - UserGroup : BetGroup
	// @Persistent
	// private UserGroup team;
	@SuppressWarnings("unused")
	@Persistent
	@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
	private Key		parentKey;

	@Persistent
	private String	name;

	public BetLabel(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getSid()
	{
		if (sid == null)
		{
			this.sid = KeyFactory.keyToString(key);
		}
		return sid;
	}

	public LabelBean toBean()
	{
		LabelBean bean = new LabelBean(name);
		bean.setId(this.getSid());
		bean.setTeamId(KeyFactory.keyToString(parentKey));
		return bean;
	}

	public Key getKey()
	{
		return key;
	}

	public UserGroup getParent(PersistenceManager pm)
	{
		return pm.getObjectById(UserGroup.class, key.getParent());
	}
}
